package com.bustle.user.dto.user;

import java.util.List;
import java.util.Vector;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.xml.bind.annotation.XmlRootElement;

@Path("/user/")
@Produces(
  {
    MediaType.APPLICATION_XML
  })
@Stateless
public class UserDtoDaoWebService
{
  @POST
  @Path("/create")
  public UserDto create(UserDto aUserDto)
  {
    userDtoDao.create(aUserDto);
    return aUserDto;
  }

  @GET
  @Path("/retrieve")
  public UserDto retrieve(@QueryParam("key") Integer aKey)
  {
    return userDtoDao.retrieve(aKey);
  }

  @GET
  @Path("/retrieveAll")
  public UserDtoList retrieveAll()
  {
    UserDtoList all = new UserDtoList();
    all.setUser(userDtoDao.retrieveAll());
    return all;
  }

  @PUT
  @Path("/update")
  public Response update(UserDto aUserDto)
  {
    userDtoDao.update(aUserDto);
    return Response.status(Status.OK).build();
  }

  @DELETE
  @Path("/delete/{id}")
  public Response delete(@PathParam("id") Integer aKey)
  {
    userDtoDao.delete(aKey);
    return Response.status(Status.OK).build();
  }

  @EJB
  protected UserDtoDao userDtoDao;

  @XmlRootElement(name = "users")
  static public class UserDtoList
  {
    public List<UserDto> getUser()
    {
      return user;
    }

    public void setUser(List<UserDto> aList)
    {
      user = aList;
    }

    protected List<UserDto> user = new Vector<>();
  }
}
